﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyMovieSystem
{
    public partial class FrmSelling : Form
    {
        public FrmSelling()
        {
            InitializeComponent();
        }

        #region 成员属性

        /// <summary>
        /// 存放影片销售信息
        /// </summary>
        Dictionary<string, Selling> dirSell = new Dictionary<string, Selling>();
        /// <summary>
        /// 存放排序后的票房
        /// </summary>
        Dictionary<string, Selling> dir_Sort = new Dictionary<string, Selling>();
        /// <summary>
        /// 存储根据日期筛选后的票房
        /// </summary>
        List<Selling> listSell = new List<Selling>();

        #endregion

        #region 加载事件
        //加载事件
        private void FrmSelling_Load(object sender, EventArgs e)
        {
            TicketManage.InitialTicket();
            MovieManage.InitialMovie();
            AddMovie();
            InitialSelling();
            SellSort();
            BindDgv();
            
        }
        #endregion

        #region 初始化信息
        /// <summary>
        /// 添加影片到销售集合中
        /// </summary>
        private void AddMovie()
        {
            foreach (Movie  item in MovieManage.dirMovie.Values)
            {
                Selling sell = new Selling(item.MovieName, 0, 0);
                dirSell.Add(sell.MovieName, sell);
            }
        }

        /// <summary>
        /// 初始化票房
        /// </summary>
        private void InitialSelling()
        {
            foreach (Movie movie in MovieManage.dirMovie.Values)
            {
                foreach (Ticket ticket in TicketManage.listTick)
                {
                    if (movie.MovieName == ticket.T_Movie.MovieName)
                    {
                        //添加影片名称
                        dirSell[movie.MovieName].MovieName = movie.MovieName;
                        //添加销售数量
                        dirSell[movie.MovieName].TicketNum++;
                        //售出日期 = 影厅放映日期
                        dirSell[movie.MovieName].Date = ticket.T_Hall.ShowData;
                    }
                }
            }
        }
        #endregion

        #region 对票房进行降序排列
        /// <summary>
        /// 对票房进行排序
        /// </summary>
        private void SellSort()
        {
            dir_Sort = dirSell.OrderByDescending(o => o.Value.TicketNum).ToDictionary(p => p.Key, o => o.Value);
            int i = 1;
            foreach (Selling item in dir_Sort.Values)
            {
                item.Rank = i;
                i++;
            }
        }
        #endregion

        #region 绑定数据
        /// <summary>
        /// 绑定dgv
        /// </summary>
        private void BindDgv()
        {
            listSell.Clear();
            DateTime time = dateTimePicker1.Value;
            int i=1;
            foreach (Selling item in dir_Sort.Values)
            {
                if (time.ToShortDateString() == item.Date.ToShortDateString())
                {
                    item.Rank = i;
                    listSell.Add(item);
                    i++;
                }
            }
            dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.DataSource = new BindingList<Selling>(listSell);
        }
        #endregion

        #region 显示票房信息

        //日期值发生改变时触发事件
        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            BindDgv();
        }

        //查询所有票房
        private void btnAll_Click(object sender, EventArgs e)
        {
            SellSort();
            dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.DataSource = new BindingList<Selling>(dir_Sort.Values.ToList());
        }

        #endregion

    }
}
